﻿using FlexiCache;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics;
using UnitTests.IISSimulator;

namespace FlexiCacheTests
{
    /// <summary>
    ///This is a test class for CachedResourceFinderTest and is intended
    ///to contain all CachedResourceFinderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CachedResourceFinderTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for ParseOutputCachedItemKey
        ///</summary>
        [TestMethod()]
        public void ParseOutputCachedItemKeyTest()
        {
            string requestUrl = @"http://www.test.com/clo/clo/assignments/courses/courseentitypresenter.rest?courseid=259899&username=Lenny";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            //string expected = "/clo/clo/assignments/courses/courseentitypresenter.rest|courseid=259899|username=Lenny";

            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                NameValueCollection vars = new NameValueCollection();
                sim.SimulateRequest(new Uri(requestUrl), vars);

                string[] elements = CachedResourceFinder.CreateMetaTagsCollection(cacheKey, System.Web.HttpContext.Current);
                Assert.AreEqual("courseid=259899", elements[1]);
                Assert.AreEqual("username=Lenny", elements[2]);
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        /// <summary>
        ///A test for ParseOutputCachedItemKey
        ///</summary>
        [TestMethod()]
        public void ParseOutputCachedItemKeyReverseOrderTest()
        {
            string requestUrl = @"http://www.test.com/clo/clo/assignments/courses/courseentitypresenter.html?username=Lenny&courseid=259899";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            //string expected = "/clo/clo/assignments/courses/courseentitypresenter.rest|courseid=259899|username=Lenny";

            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                NameValueCollection vars = new NameValueCollection();
                sim.SimulateRequest(new Uri(requestUrl), vars);

                string[] elements = CachedResourceFinder.CreateMetaTagsCollection(cacheKey, System.Web.HttpContext.Current);
                Assert.AreEqual("courseid=259899", elements[1]);
                Assert.AreEqual("username=Lenny", elements[2]);
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        /// <summary>
        ///A test for ParseOutputCachedItemKey
        ///</summary>
        [TestMethod()]
        public void ParseOutputCachedItemKeyNoParamsTest()
        {
            string requestUrl = @"/clo/clo/assignments/courses/courseentitypresenter.rest";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            //string expected = "/clo/clo/assignments/courses/courseentitypresenter.rest|courseid=259899|username=Lenny";

            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator(requestUrl, true);
                sim.SimulateRequest();

                string[] elements = CachedResourceFinder.CreateMetaTagsCollection(cacheKey, System.Web.HttpContext.Current);
                Assert.AreEqual(1, elements.Length);
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        /// <summary>
        ///A test for ParseOutputCachedItemKey
        ///</summary>
        [TestMethod()]
        public void ParseOutputCachedItemKeyWithExplicitParamTest()
        {
            string requestUrl = @"http://www.test.com/clo/clo/assignments/courses/courseentitypresenter.rest?courseid=259899&username=Lenny";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            //string expected = "/clo/clo/assignments/courses/courseentitypresenter.rest|courseid=259899|username=Lenny";

            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                NameValueCollection vars = new NameValueCollection();
                sim.SimulateRequest(new Uri(requestUrl), vars);
                System.Web.HttpContext.Current.Response.Cache.SetMetaTag("exptestHello");

                string[] elements = CachedResourceFinder.CreateMetaTagsCollection(cacheKey, System.Web.HttpContext.Current);
                Assert.AreEqual(4, elements.Length);
                Assert.AreEqual("courseid=259899", elements[1]);
                Assert.AreEqual("exptestHello", elements[2]);
                Assert.AreEqual("username=Lenny", elements[3]);
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }

        /// <summary>
        ///A test for ParseOutputCachedItemKey
        ///</summary>
        [TestMethod()]
        public void ParseOutputCachedItemKeySpeedTest()
        {
            string requestUrl = @"/clo/clo/assignments/courses/courseentitypresenter.rest?courseid=259899&username=Lenny";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            //string expected = "/clo/clo/assignments/courses/courseentitypresenter.rest|courseid=259899|username=Lenny";

            Stopwatch timer = new Stopwatch();
            HttpSimulator sim = null;
            string[] elements = null;
            try
            {
                sim = new HttpSimulator(requestUrl, true);
                sim.SimulateRequest();

                timer.Start();
                for (int ii = 0; ii < 10000; ii++)
                {
                    elements = CachedResourceFinder.CreateMetaTagsCollection(cacheKey, System.Web.HttpContext.Current);
                }
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
            timer.Stop();
            Console.WriteLine("10000 iterations - {0} ms.", timer.ElapsedMilliseconds.ToString());

        }
    }
}
// [path=/clo/clo/assignments/courses/courseentitypresenter.rest][courseid=259899][username=Lenny]